let Disabled_userHandler = function () {
}

Disabled_userHandler.prototype.ListenEvent = function () {
    const token = localStorage.getItem("JWT_TOKEN_KEY")
    const socket = io("/cmsapi", {
        auth: {
            token: token
        }
    });

    // 用户登录后加入自己的房间
    function joinUserRoom(userId) {
        socket.emit('join_user_room', {user_id: userId, token: token});
    }

    // 监听用户登录异常通知
    socket.on("error", function (result) {
        xtalert.alertErrorToast(result['message']);
    })

    // 监听用户被禁用的通知
    socket.on("user_disabled", function (data) {
        // 获取当前用户的ID
        const currentUserElement = document.getElementById("user-info");
        if (!currentUserElement) {
            console.log("无法获取当前用户信息");
            return;
        }

        const currentUserId = currentUserElement.getAttribute("data-user-id");
        if (data.count_id && data.count_id === currentUserId) {
            xtalert.alertConfirm({
                "msg": "您的账号已被禁用，即将下线",
                "showCancelButton": false,
                "confirmCallback": function () {
                    // 清除本地认证信息
                    localStorage.removeItem("JWT_TOKEN_KEY");
                    localStorage.removeItem("USER_KEY");
                    window.location.href = "/login/";
                }
            })
        }
    });

    // 获取用户ID
    const user_id = document.getElementById("user-info").getAttribute("data-user-id");
    if (user_id) {
        joinUserRoom(user_id);
    }
}

Disabled_userHandler.prototype.run = function () {
    this.ListenEvent();
}


$(function () {
    let disabled_user = new Disabled_userHandler();
    disabled_user.run()
})
